<?php
	if(!isset($rootdir)) $rootdir = "/home/nrb62281/public_html";
	include_once($rootdir . "/wowsig/DataObjects/characterData.php");
	class WoWAPIDataSource{  
		public function getCharacter($characterName, $realmName, $region){ 
			if($characterName == null){
				Throw new Exception("Character not specified.");
				return null;
			}else{
				if($realmName == null){
					Throw new Exception("Realm not specified.");
					return null;
				}else{
					if($region == null){
						Throw new Exception("Region not specified.");
						return null;
					}else{
						if((strtolower($region)=="us")||(strtolower($region)=="eu")){
							$url = "http://{$region}.battle.net/api/wow/character/{$realmName}/{$characterName}";
							if(!$this->url_exists($url)){
								Throw new Exception("URL not found:\n{$url}");
								return null;
							}else{
								$jsonString = file_get_contents($url);
								$dataObject = json_decode($jsonString, true); // force associative arrays
								if(isset($dataObject["status"])){
									if(isset($dataObject["reason"])){
										//bad realm = {"status":"nok", "reason": "Realm not found."}
										//bad character = {"status":"nok", "reason": "Character not found."}
										Throw new Exception($dataObject["reason"]);
										return null;
									}else{
										Throw new Exception("Unhandled status type received from WoWAPI: " . $dataObject["status"] . "\nURL:\n" . $url . "\nString received:\n" . $jsonString);
										return null;
									}
								}else{
									$returnValue = new characterData;
									
									$returnValue->setNamePrefix(null);
									$returnValue->setNameCharacter($dataObject["name"]);
									$returnValue->setNameSuffix(null);
									$returnValue->setGuildName(null);
									$returnValue->setLevel($dataObject["level"]);
									$returnValue->setClass($this->convertToClassStringFromInt($dataObject["class"]));
									$returnValue->setAchievementPoints($dataObject["achievementPoints"]);
									$returnValue->setFaction($dataObject["faction"]);
									$returnValue->setLifetimePVPKills(null);
									$returnValue->setRace($this->convertToRaceStringFromInt($dataObject["race"]));
									$returnValue->setGender($this->convertToGenderStringFromInt($dataObject["gender"]));
									$returnValue->setRealmName($dataObject["realm"]);
									return $returnValue;
									// $dataModel["thumbnail"] stores the URL to the thumbnail image
									// ***IMPORTANT***
									// $dataModel["lastModified"] stores time since epoch in MS that this was last modified. Used for making application get blocked less by the API website.
								}
							}
						}else{
							Throw new Exception("Region {$region} not supported.");
							return null;
						}
					}
				}
			}			
		}
		public function url_exists($url) {
			// Version 4.x supported
			$handle   = curl_init($url);
			if (false === $handle)
			{
				return false;
			}
			curl_setopt($handle, CURLOPT_HEADER, false);
			curl_setopt($handle, CURLOPT_FAILONERROR, true);  // this works
			curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") ); // request as if Firefox    
			curl_setopt($handle, CURLOPT_NOBODY, true);
			curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
			$connectable = curl_exec($handle);
			curl_close($handle);   
			return $connectable;
		}
		public function convertToGenderStringFromInt($genderInt){
		//english
			switch($genderInt){
				case 1:
					return "female";
					break;
				case 0:
					return "male";
					break;
				default:
					Throw new Exception("Unknown Gender: {$genderInt}");
					break;
			}
		}
		public function convertToRaceStringFromInt($raceInt){
		//english
			switch($raceInt){
				case 1:
					return "Human";
					break;
				case 2:
					return "Orc";
					break;
				case 3:
					return "Dwarf";
					break;
				case 4:
					return "Night Elf";
					break;
				case 5:
					return "Undead";
					break;
				case 6:
					return "Tauren";
					break;
				case 7:
					return "Gnome";
					break;
				case 8:
					return "Troll";
					break;
				case 9:
					return "Goblin";
					break;
				case 10:
					return "Blood Elf";
					break;
				case 11:
					return "Draenei";
					break;
				case 22:
					return "Worgen";
					break;
				default:
					Throw new Exception("Unknown Race: {$raceInt}");
					break;
			}
		}
		public function convertToClassStringFromInt($classInt){
		//english
			switch($classInt){
				case 1:
					return "Warrior";
					break;
				case 2:
					return "Paladin";
					break;
				case 3:
					return "Hunter";
					break;
				case 4:
					return "Rogue";
					break;
				case 5:
					return "Priest";
					break;
				case 6:
					return "Death Knight";
					break;
				case 7:
					return "Shaman";
					break;
				case 8:
					return "Mage";
					break;
				case 9:
					return "Warlock";
					break;
				case 11:
					return "Druid";
					break;
				// druid is case 11, this is not a typo.
				default:
					Throw new Exception("Unknown Class: {$classInt}");
					break;
			}
		}
	}
?>